package com.ymatou.iapi.m.couponservice.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.UserReceiveResultInfo;
import com.ymatou.iapi.couponservice.parameter.UserReceiveResultList;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.SendSmallCouponBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.SendSmallCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.TestDataManager;
/**
*商家小批量发放优惠券用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendSmallCoupon{
	private static SendSmallCouponBean sendsmallcouponBean;
	private static SendSmallCouponCall sendsmallcouponCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家小批量发放优惠券用例");
	}

	@Before
	public void caseUp() {
		sendsmallcouponBean=new SendSmallCouponBean();
		sendsmallcouponCall=new SendSmallCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendsmallcouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_001() {
		Logger.start(true,"商家小批量发放优惠券");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
			
			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();			
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_003() {
		Logger.start(true,"反例-批次编号不能为空");
		try {
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
		
			sendsmallcouponBean.setBatchCode(null);

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("1", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("batchCode:批次编号不能为空", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_004() {
		Logger.start(true,"反例-请传入要发放给哪些用户-null");
		try {
			

			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
			
			sendsmallcouponBean.setUserIds(null);

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("1", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("userIds:请传入要发放给哪些用户", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_005() {
		Logger.start(true,"反例-请传入要发放给哪些用户-空list");
		try {

			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
			
			sendsmallcouponBean.setUserIds(new ArrayList<>());

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("1", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("userIds:请传入要发放给哪些用户并且最多不超过20个用户", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_006() {
		Logger.start(true,"反例-请提供商家ID");
		try {

			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
			
			sendsmallcouponBean.setSellerId(null);

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("2", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("平台不允许发放商家券", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_007() {
		Logger.start(true,"反例-超过20个用户");
		try {

			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
			
			List<Integer> userIds = new ArrayList<>();
			
			for(int i=0;i<21;i++)
			{
				userIds.add(TestDataManager.getRandomIntNum(8));
			}

			sendsmallcouponBean.setUserIds(userIds);

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			Logger.verifyEquals("1", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("userIds:请传入要发放给哪些用户并且最多不超过20个用户", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_008() {
		Logger.start(true,"反例-批次不存在");
		try {

			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
			
			sendsmallcouponBean.setBatchCode("99999999");

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("2", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次不存在", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_009() {
		Logger.start(true,"反例-商家创建发放任务，但批次不属于商家创建");
		try {

			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
			
			sendsmallcouponBean.setSellerId(1111);

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("2", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("商家发放优惠券，发放的批次不属于此商家", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_010() {
		Logger.start(true,"反例-创建发放任务，商家不允许发放平台券");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			 CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			 CreateCouponCall createCouponCall = couponServiceMCallService.createPlatformCoupon(createCouponBean);
						
				List<Integer> userIds = new ArrayList<>();
				
				userIds.add(TestDataManager.getRandomIntNum(8));
				userIds.add(TestDataManager.getRandomIntNum(8));
				userIds.add(TestDataManager.getRandomIntNum(8));

			sendsmallcouponBean = couponServiceMCallService.setSendsmallcouponBean(userIds, createCouponCall.getBatchCode(), 3383, "dddd");;

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("2", sendsmallcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("商家不允许发放平台券", sendsmallcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_011() {
		Logger.start(true,"反例-批次已作废");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
		
			couponServiceMCallService.InvalidSellerBatch(sendsmallcouponBean.getSellerId(),sendsmallcouponBean.getBatchCode());

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("0", sendsmallcouponCall.getCode(), "验证返回code");

			Logger.verifyEquals("操作成功！", sendsmallcouponCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = sendsmallcouponCall.getUserReceiveResultList();
			
			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
			
			List<Integer> users=sendsmallcouponBean.getUserIds();
			List<Integer> userids=new ArrayList<>();
			
			// 验证返回
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);

				userids.add(item.getUserId());
				
				Logger.verifyEquals(sendsmallcouponBean.getBatchCode(), item.getBatchCode(), "验证返回BatchCode");
				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
				Logger.verifyEquals(4, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
			}
			Logger.verifyEquals(true,TestDataManager.isListEqual(userids, users), "验证返回UserId");
		
			// 验证数据库
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sendsmallcouponBean.getBatchCode()).get(0);
			List<YmtCoupon> coupons  =  new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
			
			Logger.verifyEquals(0, coupons.size(), "验证数据库生成Coupon条数正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_012() {
		Logger.start(true,"反例-批次已过期");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
		
			 // 设置优惠券过期
            Date date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
            int iCouponSettingId =
                    new YmtCouponBatchWapper().selectBysBatchCode(sendsmallcouponBean.getBatchCode()).get(0).getiCouponSettingId();
            new YmtCouponSettingWapper().setiEffectiveType(iCouponSettingId, 0);
            new YmtCouponSettingWapper().setdValidEnd(iCouponSettingId, date);
            
			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("0", sendsmallcouponCall.getCode(), "验证返回code");

			Logger.verifyEquals("操作成功！", sendsmallcouponCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = sendsmallcouponCall.getUserReceiveResultList();
			
			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
			
			List<Integer> users=sendsmallcouponBean.getUserIds();

			List<Integer> userids=new ArrayList<>();
			
			// 验证返回
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);

				userids.add(item.getUserId());
				Logger.verifyEquals(sendsmallcouponBean.getBatchCode(), item.getBatchCode(), "验证返回BatchCode");
				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
				Logger.verifyEquals(3, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
			}
			
			Logger.verifyEquals(true,TestDataManager.isListEqual(userids, users), "验证返回UserId");
			
			// 验证数据库
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sendsmallcouponBean.getBatchCode()).get(0);
			List<YmtCoupon> coupons  =  new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
			
			Logger.verifyEquals(0, coupons.size(), "验证数据库生成Coupon条数正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmallCoupon_013() {
		Logger.start(true,"反例-批次已领完");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();			
			sendsmallcouponBean = couponServiceMCallService.setSendSmallCouponBean();
		
			  YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sendsmallcouponBean.getBatchCode()).get(0);
			  int iCouponSettingId =
	                    new YmtCouponBatchWapper().selectBysBatchCode(sendsmallcouponBean.getBatchCode()).get(0).getiCouponSettingId();
	            new YmtCouponSettingWapper().setiReceiveCount(iCouponSettingId, batch.getiCouponTotalNum());
	            

			sendsmallcouponCall.setData(sendsmallcouponBean);
			sendsmallcouponCall.callService();	
			
			Logger.verifyEquals("0", sendsmallcouponCall.getCode(), "验证返回code");

			Logger.verifyEquals("操作成功！", sendsmallcouponCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = sendsmallcouponCall.getUserReceiveResultList();
			
			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
			
			List<Integer> users=sendsmallcouponBean.getUserIds();
			
			// 验证返回
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);
				Logger.verifyEquals(sendsmallcouponBean.getBatchCode(), item.getBatchCode(), "验证返回BatchCode");
				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
				Logger.verifyEquals(1, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
			}
			
			// 验证数据库
			YmtCouponBatch batch1 = new YmtCouponBatchWapper().selectBysBatchCode(sendsmallcouponBean.getBatchCode()).get(0);
			List<YmtCoupon> coupons  =  new YmtCouponWapper().selectByiBatchId(batch1.getiBatchId());
			
			Logger.verifyEquals(0, coupons.size(), "验证数据库生成Coupon条数正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn()
	{
		Logger.verifyEquals("0", sendsmallcouponCall.getCode(), "验证返回code");

		Logger.verifyEquals("操作成功！", sendsmallcouponCall.getMessage(), "验证返回Message");

		UserReceiveResultList userReceiveResultList = sendsmallcouponCall.getUserReceiveResultList();
		
		List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
		
		List<Integer> users=sendsmallcouponBean.getUserIds();
		
		List<Integer> userids = new ArrayList<>();
		
		// 验证返回
		for(int i=0;i<list.size();i++)
		{
			UserReceiveResultInfo item = list.get(i);
			Logger.verifyEquals(sendsmallcouponBean.getBatchCode(), item.getBatchCode(), "验证返回BatchCode");
			Logger.verifyEquals(true, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
			Logger.verifyEquals(0, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
			userids.add(item.getUserId());
		}
		
		Logger.verifyEquals(true,TestDataManager.isListEqual(userids, users), "验证返回UserId");
		
		// 验证数据库
		YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sendsmallcouponBean.getBatchCode()).get(0);
		List<YmtCoupon> coupons  =  new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
		
		Logger.verifyEquals(users.size(), coupons.size(), "验证数据库生成Coupon条数正确");
		
		for(int i=0;i<coupons.size();i++)
		{
			YmtCoupon coupon = coupons.get(i);
			
			// iCouponSetting
			Logger.verifyEquals(batch.getiCouponSettingId(), coupon.getiCouponSetting(),
					"验证数据库表Ymt_Coupon表中iCouponSetting");

			// 优惠券类型 2：链接
			Logger.verifyEquals(2, coupon.getiCouponType(), "验证数据库表Ymt_Coupon表中iCouponType");
			// 1：普通优惠卷
			Logger.verifyEquals(1, coupon.getiUsage(), "验证数据库表Ymt_Coupon表中iUsage");

			List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper()
					.selectBysCouponId(coupon.getsCouponId());

			Logger.verifyEquals(1, ymtCouponPrivateUserBounds.size(),
					"验证数据库表ymt_CouponPrivateUserBound生成一条sCouponId=" + coupon.getsCouponId() + "的记录");
			
			YmtCouponPrivateUserBound ymtCouponPrivateUserBound = ymtCouponPrivateUserBounds.get(0);
	
			users.remove(ymtCouponPrivateUserBound.getiUserId());
			
			Logger.verifyEquals(batch.getiBatchId(), ymtCouponPrivateUserBound.getiBatchId(),
					"验证数据库表ymt_CouponPrivateUserBound中iBatchId正确");
		}
		
		Logger.verifyEquals(0, users.size(), "验证数据库表ymt_CouponPrivateUserBound生成了传人所有SendUsers的优惠券");

	}
}